﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AIVision.Model.MachineModels;
using AIVision.Model.UIModels;
using System.Collections.ObjectModel;
using LiveCharts;
using LiveCharts.Wpf;
using LiveCharts.Definitions.Series;
using Prism.Regions;
using System.Threading;
using System.Windows.Threading;
using Prism.Events;
using AIVision.Model.EntityModels;
using Prism.Commands;
using AIVision.Repository.Services;
using AIVision.Repository.Entitys;
using AIVision.Model.InspectModels;
namespace AIVision.UI.ViewModels
{
    public class DataViewModel: BindableBase
    {
        private readonly IEventAggregator eventAggregator;
        private readonly InpsectionRecordService inpsectionRecordService;

        public DelegateCommand BtnQueryRecordCmd { get; private set; }
        public DelegateCommand BtnSaveClearCmd { get; private set; }
        Timer timer;
        private SummaryDataModel summaryData;
        public SummaryDataModel SummaryData
        {
            get { return summaryData; }
            set { summaryData = value; RaisePropertyChanged(); }
        }


        private SeriesCollection series = new SeriesCollection();
        public SeriesCollection Series
        {
            get { return series; }
            set { series = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<InpsectionRecordDto> inpsectionRecords;
        public ObservableCollection<InpsectionRecordDto> InpsectionRecords
        {
            get { return inpsectionRecords; }
            set { inpsectionRecords = value; RaisePropertyChanged(); }
        }

        private DateTime startTime = DateTime.Now.AddDays(-1);
        public DateTime StartTime
        {
            get { return startTime ; }
            set { startTime  = value; RaisePropertyChanged(); }
        }

        private DateTime endTime = DateTime.Now;
        public DateTime EndTime
        {
            get { return endTime; }
            set { endTime = value; RaisePropertyChanged(); }
        }



        public DataViewModel(SummaryDataModel summaryDataModel, IEventAggregator eventAggregator, InpsectionRecordService inpsectionRecordService)
        {
            this.SummaryData = summaryDataModel;
            this.eventAggregator = eventAggregator;
            this.inpsectionRecordService = inpsectionRecordService;
            timer = new Timer(timerCallBack, null, 1000, 60000);
            Series.Add(
                new PieSeries
                {
                    Title = "合格品",
                    Values = new ChartValues<double> { 100 },
                    DataLabels = true,
                    LabelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation)
                }
            );

            BtnQueryRecordCmd = new DelegateCommand(SearchInspectionRecord);
            BtnSaveClearCmd = new DelegateCommand(SaveInspectionRecord);
        }

        void timerCallBack(object state)
        {
            eventAggregator.GetEvent<UpdatePieSericesEvent>().Publish();
        }

        private void SaveInspectionRecord()
        {
            inpsectionRecordService.SaveRecordAndRefSummary(SummaryData);
        }

        private void SearchInspectionRecord()
        {
             InpsectionRecords = inpsectionRecordService.GetRecords(StartTime,EndTime);
        }
    }
    

    public class UpdatePieSericesEvent:PubSubEvent
    {

    }
}
